package com.sjzsqjy.asdtiang.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.ShareService;
import com.sjzsqjy.asdtiang.mysql.FeedBack;
import com.sjzsqjy.asdtiang.mysql.HosRole;
import com.sjzsqjy.asdtiang.mysql.HosUser;
import com.sjzsqjy.asdtiang.mysql.Info;
import com.sjzsqjy.asdtiang.mysql.Link;

public class AdminAction extends ActionSupport {
	private Info info;
	private String infoId;
	private String id;
	private List list;
	private HosUser hosUser;
	private ShareService shareService;
	private Object object;
	private String objectName;
	private FeedBack feedBack;
	private String result;
	private String hosUserType;
	private Link link;

	public Link getLink() {
		return link;
	}

	public void setLink(Link link) {
		this.link = link;
	}

	public String getHosUserType() {
		return hosUserType;
	}

	public void setHosUserType(String hosUserType) {
		this.hosUserType = hosUserType;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	private int pageNow = 1; //
	private int pageSize = 10; //
	private int pageTotle = 1;//

	public HosUser getHosUser() {
		return hosUser;
	}

	public void setHosUser(HosUser hosUser) {
		this.hosUser = hosUser;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageTotle() {
		return pageTotle;
	}

	public void setPageTotle(int pageTotle) {
		this.pageTotle = pageTotle;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}

	public String getInfoId() {
		return infoId;
	}

	public void setInfoId(String infoId) {
		this.infoId = infoId;
	}

	public ShareService getShareService() {
		return shareService;
	}

	public void setShareService(ShareService shareService) {
		this.shareService = shareService;
	}

	public String listObjectExecute() throws Exception {

		return "edit";
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String editExecute() throws Exception {

		if (objectName.equals("FeedBack")) {
			object = shareService.getObjectById(objectName, id);
			feedBack = (FeedBack) object;
		}
		if (objectName.equals("HosUser")) {
			object = shareService.getObjectById(objectName, Long.parseLong(id));
			hosUser = (HosUser) object;
		}
		if (objectName.equals("Info")) {
			object = shareService.getObjectById(objectName, id);
			info= (Info) object;
		}
		return "edit" + objectName;
	}

	public String listAllExecute() throws Exception {
		String order = "";
		List l = shareService.listAllByObjectName(objectName, order);
		if (l.size() == 0) {
			return "noview";
		}
		if (l.size() > pageSize) {
			list = l.subList(0, pageSize);
		} else {
			list = l;
		}
		ActionContext.getContext().getSession().put("list", l);
		ActionContext.getContext().getSession()
				.put("view", "list" + objectName);
		int i = l.size();
		System.out.println("size:    " + i);
		pageTotle = i % pageSize == 0 ? i / pageSize : i / pageSize + 1;
		// System.out.println("show List");
		return "list" + objectName;
	}

	public String saveExecute() throws Exception {
		String isPower = adminExecute();
		if (isPower.equals("error")) {
			return "noPower";
		}
		if (objectName.equals("HosUser")) {
			hosUser = HosUser.validate(hosUser, this);

			if (null == hosUser) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "create" + objectName;
			}
			Set<HosRole> hosRoles = new HashSet<HosRole>(0);
			hosUserType = "ROLE_诊所";
			if (hosUserType.equals("ROLE_管理员")) {
				object = shareService.getObjectById("HosRole", 1l);
			} else {
				object = shareService.getObjectById("HosRole", 2l);
			}
			hosRoles.add((HosRole) object);
			hosUser.setHosRoles(hosRoles);
			shareService.save(hosUser);
		}
		if (objectName.equals("Info")) {
			info = Info.validate(info, this);
			if (null == info) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "create" + objectName;
			}
			shareService.save(info);
		}
		if (objectName.equals("Link")) {
			link = Link.validate(link, this);
			if (null == link) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "create" + objectName;
			}
			shareService.save(link);
		}
		result = "保存成功或更新成功";
		return "saveOk";
	}

	public String updateExecute() throws Exception {
		String isPower = adminExecute();
		if (isPower.equals("error")) {
			return "noPower";
		}
		if (objectName.equals("FeedBack")) {
			shareService.update(feedBack);
		}
		if (objectName.equals("HosUser")) {
			hosUser = HosUser.validate(hosUser, this);
			if (null == hosUser) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "create" + objectName;
			}
			shareService.update(hosUser);
		}
		if (objectName.equals("Info")) {
			info = Info.validate(info, this);
			if (null == info) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "edit" + objectName;
			}
			shareService.update(info);
		}
		if (objectName.equals("Link")) {
			link = Link.validate(link, this);
			if (null ==link) {
				this.addFieldError("null", this.getText("toUsEmpty"));
				return "create" + objectName;
			}
			shareService.update(link);
		}
		result = "保存成功或更新成功";
		return "saveOk";
	}

	public String deleteExecute() throws Exception {

		return "delete";
	}

	public String createExecute() throws Exception {

		return "create";
	}

	public String adminExecute() throws Exception {
		System.out.println("ddd"
				+ ActionContext.getContext().getSession().get("userRole")
				+ "ss");
		if (null != ActionContext.getContext().getSession().get("userRole")
				&& ActionContext.getContext().getSession().get("userRole")
						.equals("ROLE_管理员")) {
			return SUCCESS;
		}
		return ERROR;

	}
}
